package Simple4;

import java.util.HashMap;

public class StateUtils {
	/*************Explanation***********
	 *  I think my code is better because 
	 *  1)  I used two HashMaps to restore the
	 *  	(state code----state name) and (state name----state code) pairs in terms
	 *  	of that we could run a quick check about the relationship of it.
	 *  	Besides, if here is some change in the state selection, we could update via
	 *  	the hashmap easily.
	 *  2)  I made the the code more re-usable, and more concise and clear.
	 *  3)  The query process time complexity of hashmap is O(1) but it sacrifices some
	 *  	space complexity.
	 *  4)  I think we could do better to use a trie instead of two hashmap, but I think
	 *  	for 50 states, there is no need to build such a new data structure.
	 *  5)  Use a StringBuilder instead to make the code more clear.
	 *  6)  I think the HTML select option value should be like 
	 *  	<option value="AL">Alabama</option>
	 *  	which starts with the abbreviation of each state to make it clear.
	 *************Explanation**********/
	
	private static String OPTION = "<option value=\"%s\">%s</option>\n";
	private static String SELECT = "<select name=\"state\">\n%s</select>\n";
	private static HashMap<String, String> code_name = new HashMap<String,String>();
	private static HashMap<String, String> name_code = new HashMap<String,String>();
	private static String[] states = { "Alabama","Alaska","Arizona","Arkansas","California","Colorado",
			"Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois",
			"Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland",
			"Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana",
			"Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York",
			"North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania",
			"Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah",
			"Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"
			};
	private static String[] abbr = {"AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA",
			 "HI","ID","IL","IN","IA","KS","KY","LA","ME","MD",
			 "MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ",
			 "NM","NY","NC","ND","OH","OK","OR","PA","RI","SC",
			 "SD","TN","TX","UT","VT","VA","WA","WV","WI","WY"
			};

	/**
	 * Constructor.
	 */
	public StateUtils(){
		createmap();
	}
	
	private void createmap(){
		for(int i=0;i<abbr.length;i++){
			code_name.put(abbr[i], states[i]);
			name_code.put(states[i], abbr[i]);
		}
	}
	
	/**
	 * Generate an HTML select list that can be used to select a 
	 * specific U.S. state.
	 * @return
	 */
	public static String createStateSelectList(){
		//Use String.format function to printout the result like
		// c version of printf
	    StringBuilder sb = new StringBuilder();
	    for(int i=0;i<abbr.length;++i){
	    	sb.append(String.format(OPTION, abbr[i],displayStateFullName(abbr[i])));
	    }
	    return String.format(SELECT, sb.toString());
	}
	
	/**
	 * Parses the state from an HTML form submission, converting it to
	 * the two-letter abbreviation. We need to store the two-letter abbreviation
	 * in our database.
	 * @param s
	 * @return
	 */
	public static String parseSelectedState(String s){
	    if(name_code.containsKey(s))
	    	return name_code.get(s);
	    return null;
	}
	
	/**
	 * Displays the full name of the state specified by two-letter code.
	 * @param abbr
	 * @return
	 */
	public static String displayStateFullName(String abbr){
		if(code_name.containsKey(abbr))
			return code_name.get(abbr);
		return null;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		StateUtils test = new StateUtils();
		System.out.println(test.createStateSelectList());
	}

}
